#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "m_logs.h"

/* initialize logs */
int m_log_init(void)
{

    return 0;
}


void m_log(int log_level, const char *fmt, ...)
{
    /* Guess we need no more than 100 bytes. */
    int n, size = 100;
    char *p, *np;
    va_list ap;

    if ((p = malloc(size)) == NULL)
        return ;

    while (1)
    {
        /* Try to print in the allocated space. */
        va_start(ap, fmt);
        n = vsnprintf(p, size, fmt, ap);
        va_end(ap);
        /* If that worked, return the string. */
        if (n > -1 && n < size)
            break;
        /* Else try again with more space. */
        if (n > -1) /* glibc 2.1 */
            size = n + 1; /* precisely what is needed */
        else /* glibc 2.0 */
            size *= 2; /* twice the old size */
        if ((np = realloc(p, size)) == NULL)
        {
            free(p);
            return ;
        } else
        {
            p = np;
        }
    }
    
    switch(log_level)
    {
        case LOG_ALL:
            break;
        case LOG_ERROR:
            break;
        case LOG_DEBUG:
            break;
        case LOG_WARN:
            break;
        default:
            break;
    }
    
    fprintf(stderr, "%s", p);
    if(p)
    {
        free(p);
        p = NULL;
    }
    return ;
}
